/** pay.js 提供支付组件，包括普通商品支付以及虚拟商品支付*/
//正常商品支付接口
function toPay(order, addressId){
  wx.request({
    url: getApp().globalData.host + 'webapp/pay/toPay.html',
    data: { orderId: order.orderId, addressId: addressId},
    header: getApp().globalData.header,
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && typeof (res.data.prepayId) != 'undefined') {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.prepayId,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.redirectTo({
              url: '/pages/payinfo/payinfo?payAmount=' + order.payableAmount + '&orderCode=' + order.orderCode
            })
          },
          'fail': function (res) {
            wx.showToast({
              title: "支付失败！",
              duration: 2000
            })
          }
        })
      } else {
        wx.showModal({
          title: '预定失败！',
          content:res.data.errorMsg,
          showCancel:false
        })
      }
    },
    fail: function (res) {
      wx.showToast({
        title: res.data,
      })
    },
    complete: function (res) { }
  })
}

//优惠券支付接口
function toReducePay(order, addressId, couponItemId, payAmount) {
  if (typeof (couponItemId) == 'undefined' || couponItemId == 'undefined' )
    couponItemId = '';
  wx.request({
    url: getApp().globalData.host + 'webapp/pay/toPay.html',
    data: { orderId: order.orderId, addressId: addressId, couponItemId: couponItemId },
    header: getApp().globalData.header,
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && typeof (res.data.prepayId) != 'undefined') {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.prepayId,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.redirectTo({
              url: '/pages/payinfo/payinfo?payAmount=' + payAmount + '&orderCode=' + order.orderCode
            })
          },
          'fail': function (res) {
            wx.showToast({
              title: "支付失败！",
              duration: 2000
            })
          }
        })
      } else {
        wx.showModal({
          title: '预定失败！',
          content:res.data.errorMsg,
          showCancel:false
        })
      }
    },
    fail: function (res) {
      wx.showToast({
        title: res.data,
      })
    },
    complete: function (res) { }
  })
}

//虚拟商品支付接口
function specialPay(order){
  wx.request({
    url: getApp().globalData.host + 'webapp/pay/specialPay.html',
    data: { orderId: order.orderId},
    header: getApp().globalData.header,
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && typeof (res.data.prepayId) != 'undefined') {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.prepayId,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.redirectTo({
              url: '/pages/payinfo/payinfo?payAmount=' + order.payableAmount + '&orderCode=' + order.orderCode
            })
          },
          'fail': function (res) {
            wx.showToast({
              title: "支付失败！",
              duration: 2000
            })
          }
        })
      } else {
        wx.showToast({
          title: "支付失败！",
          duration: 2000
        })
      }
    },
    fail: function (res) {
      wx.showToast({
        title: res.data,
      })
    },
    complete: function (res) { }
  })
}

module.exports = {
  toPay: toPay,
  toReducePay: toReducePay,
  specialPay: specialPay
}